package com.example.service.impl;

import com.example.entity.SysUser;
import com.example.service.ISysUserService;
import org.nutz.dao.Cnd;
import org.nutz.dao.Dao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;

import java.util.List;

/**
 * <p>
 * 系统用户 服务实现类
 * </p>
 *
 * @author lilufeng
 * @since 2016-11-28
 */
@Service
public class SysUserServiceImpl implements ISysUserService {

	@Autowired
	private Dao dao;

	@Cacheable(value = "sysUsers",key = "'sys111'")
	@Override
	public List<String> queryAllPerms(Long userId) {
		return null;
	}

	@Override
	public boolean add(SysUser sysUser) {
		String password = DigestUtils.md5DigestAsHex(sysUser.getPassword().getBytes());
		sysUser.setPassword(password);
		dao.insert(sysUser);
		return true;
	}

	@Override
	public SysUser query(SysUser sysUser) {
		return dao.fetch(SysUser.class, Cnd.where("username", "=", sysUser.getUsername()));
	}

	@Override
	public List<SysUser> selectPage(int current, int pageSize) {
		return dao.query(SysUser.class, null, dao.createPager(current, pageSize));
	}

	@Override
	public SysUser queryByUsername(String username) {
		return dao.fetchLinks(dao.fetch(SysUser.class,Cnd.where("username", "=", username)), "sysRoles");
	}

}
